home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrtools-1.10 / lib / stdio / filewrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-20  |  2.1 KB  |  96 lines

  1. /* @(#)filewrite.c    1.9 97/04/20 Copyright 1986 J. Schilling */
  2. /*
  3.  *    Copyright (c) 1986 J. Schilling
  4.  */
  5. /*
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include "io.h"
  23.  
  24. static    char    _writeerr[]    = "file_write_err";
  25.  
  26. #ifdef    HAVE_USG_STDIO
  27.  
  28. int filewrite (f, vbuf, len)
  29.     register FILE    *f;
  30.     void    *vbuf;
  31.     int    len;
  32. {
  33.     register int    n;
  34.     int    cnt;
  35.     char        *buf = vbuf;
  36.  
  37.     down2(f, _IOWRT, _IORW);
  38.  
  39.     if (f->_flag & _IONBF){
  40.         cnt = write (fileno(f), buf, len);
  41.         if (cnt < 0){
  42.             f->_flag |= _IOERR;
  43.             if (!(my_flag(f) & _IONORAISE))
  44.                 raisecond(_writeerr, 0L);
  45.         }
  46.         return cnt;
  47.     }
  48.     cnt = 0;
  49.     while (len > 0) {
  50.         if (f->_cnt <= 0){
  51.             if (_flsbuf((unsigned char) *buf++, f) == EOF)
  52.                 break;
  53.             cnt++;
  54.             if (--len == 0)
  55.                 break;
  56.         }
  57.         if ((n = f->_cnt >= len ? len : f->_cnt) > 0) {
  58.             f->_ptr = (unsigned char *)movebytes (buf, f->_ptr, n);
  59.             buf += n;
  60.             f->_cnt -= n;
  61.             cnt += n;
  62.             len -= n;
  63.         }
  64.     }
  65.     if (!ferror(f))
  66.         return cnt;
  67.     if (!(my_flag(f) & _IONORAISE))
  68.     raisecond(_writeerr, 0L);
  69.     return -1;
  70. }
  71.  
  72. #else
  73.  
  74. int filewrite (f, vbuf, len)
  75.     register FILE    *f;
  76.     void    *vbuf;
  77.     int    len;
  78. {
  79.     int    cnt;
  80.     char        *buf = vbuf;
  81.  
  82.     down2(f, _IOWRT, _IORW);
  83.  
  84.     if (my_flag(f) & _IOUNBUF)
  85.         return write(fileno(f), buf, len);
  86.     cnt = fwrite(buf, 1, len, f);
  87.  
  88.     if (!ferror(f))
  89.         return cnt;
  90.     if (!(my_flag(f) & _IONORAISE))
  91.     raisecond(_writeerr, 0L);
  92.     return -1;
  93. }
  94.  
  95. #endif    /* HAVE_USG_STDIO */
  96.